package com.jincheng.web.controller.system;

import com.jincheng.common.annotation.Log;
import com.jincheng.common.core.controller.BaseController;
import com.jincheng.common.core.domain.AjaxResult;
import com.jincheng.common.core.page.TableDataInfo;
import com.jincheng.common.enums.BusinessType;
import com.jincheng.common.utils.poi.ExcelUtil;
import com.jincheng.system.domain.SignInSheet;
import com.jincheng.system.domain.dto.SignInSheetScreenDTO;
import com.jincheng.system.domain.dto.SignInSheetSimpleDTO;
import com.jincheng.system.service.ISignInSheetService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 签到记录Controller
 *
 * @author DairyJay
 * @date 2025-02-24
 */
@RestController
@RequestMapping("/system/signInSheet")
public class SignInSheetController extends BaseController {
    private final ISignInSheetService signInSheetService;

    public SignInSheetController(ISignInSheetService signInSheetService) {
        this.signInSheetService = signInSheetService;
    }
    /**
     * 查询签到记录列表
     */
//    @PreAuthorize("@ss.hasRole('system:activity:list')")
    @PreAuthorize("@ss.lacksRole('common')")
    @GetMapping("/list")
    public TableDataInfo list(SignInSheet signInSheet) {
        startPage();
        List<SignInSheet> list = signInSheetService.selectList(signInSheet);
        return getDataTable(list);
    }


    /**
     * 根据活动id查询签到记录列表
     *
     * @param signInSheetScreenDTO 筛选列表
     * @return 签到记录列表
     */
    @PreAuthorize("@ss.lacksRole('common')")
    @GetMapping("/listByActivity/{activityId}")
    public TableDataInfo listByActivity(SignInSheetScreenDTO signInSheetScreenDTO) {
        List<SignInSheetSimpleDTO> list = signInSheetService.selectSignInSheetByActivityId(signInSheetScreenDTO);
        return getDataTable(list);
    }

    /**
     * 根据活动id和用户id查询签到记录
     *
     * @param userId     用户id
     * @param activityId 活动id
     * @return 签到记录
     */
    @GetMapping("/byActivityUser")
    public AjaxResult byActivityUser(@RequestParam("userId") Long userId,
                                     @RequestParam("activityId") Long activityId) {
        SignInSheet signInSheet = signInSheetService.selectSignInSheetByActivityUser(userId, activityId);
        return success(signInSheet);
    }


    /**
     * 报名信息统计
     *
     * @param activityId 活动id
     * @return 统计信息
     */
    @GetMapping("/peopleCounting/{activityId}")
    public AjaxResult peopleCounting(@PathVariable Long activityId) {
        return AjaxResult.success(signInSheetService.peopleCounting(activityId));
    }

    /**
     * 导出签到记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:activity:export')")
    @Log(title = "签到记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SignInSheet signInSheet) {
        List<SignInSheet> list = signInSheetService.selectList(signInSheet);
        ExcelUtil<SignInSheet> util = new ExcelUtil<>(SignInSheet.class);
        util.exportExcel(response, list, "签到记录数据");
    }

    /**
     * 获取签到记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:activity:query')")
    @GetMapping(value = "/{checkInId}")
    public AjaxResult getInfo(@PathVariable("checkInId") Long checkInId) {
        return success(signInSheetService.selectSignInSheetByCheckInId(checkInId));
    }

    /**
     * 新增签到记录
     */
    @PreAuthorize("@ss.hasPermi('system:activity:add')")
    @Log(title = "签到记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SignInSheet signInSheet) {
        return toAjax(signInSheetService.insertSignInSheet(signInSheet));
    }

    /**
     * 修改签到记录
     */
    @PreAuthorize("@ss.hasPermi('system:activity:edit')")
    @Log(title = "签到记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SignInSheet signInSheet) {
        return toAjax(signInSheetService.updateSignInSheet(signInSheet));
    }


    /**
     * 修改签到记录状态
     */
    @PreAuthorize("@ss.hasPermi('system:activity:edit')")
    @Log(title = "签到记录", businessType = BusinessType.UPDATE)
    @PutMapping("/status/{status}")
    public AjaxResult editCheckStatus(@RequestBody List<SignInSheet> signInSheetIdList,
                                      @PathVariable Integer status) {
        if (signInSheetIdList == null || signInSheetIdList.isEmpty())
            return success();
        return toAjax(signInSheetService.updateSignInSheetCheckStatus(signInSheetIdList, status));
    }


    /**
     * 删除签到记录
     */
    @PreAuthorize("@ss.hasPermi('system:activity:remove')")
    @Log(title = "签到记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{checkInIds}")
    public AjaxResult remove(@PathVariable Long[] checkInIds) {
        return toAjax(signInSheetService.deleteSignInSheetByCheckInIds(checkInIds));
    }
}
